@ECHO off

REM Setting up devenv path
echo Checking MSVC version

SET MSVCROOTKEY=HKLM\SOFTWARE\Microsoft\VisualStudio
SET MSVC71KEY=%MSVCROOTKEY%\7.1\Setup\VC
SET MSVC8KEY=%MSVCROOTKEY%\8.0\Setup\VC
SET MSVC8EXPRESSKEY=HKLM\SOFTWARE\Microsoft\VCExpress\8.0\Setup\VC

REM First see if we can find MSVC, then set the variable
REM NOTE: delims=<tab><space>
REM NOTE: run the initial REQ QUERY outside of the if() to set ERRORLEVEL correctly

REG QUERY "%MSVC71KEY%" /v ProductDir >nul 2>nul
if "%VC71DIR%"=="" (
  IF %ERRORLEVEL% EQU 0 (
    FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%MSVC71KEY%" /v ProductDir') DO SET VC71DIR=%%B
  )
)

REG QUERY "%MSVC8KEY%" /v ProductDir >nul 2>nul
if "%VC8DIR%"=="" (
  IF %ERRORLEVEL% EQU 0 (
    FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%MSVC8KEY%" /v ProductDir') DO SET VC8DIR=%%B
  )
)

REG QUERY "%MSVC8EXPRESSKEY%" /v ProductDir >nul 2>nul
if "%VC8EXPRESSDIR%"=="" (
  IF %ERRORLEVEL% EQU 0 (
    FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%MSVC8EXPRESSKEY%" /v ProductDir') DO SET VC8EXPRESSDIR=%%B
  )
)

if not "%VC8EXPRESSDIR%"=="" (
  set VCDIR=%VC8EXPRESSDIR%
  set PROJ=quake3\code\win32\msvc8
)
if not "%VC71DIR%"=="" (
  set VCDIR=%VC71DIR%
  set PROJ=quake3\code\win32\msvc
)
if not "%VC8DIR%"=="" (
  set VCDIR=%VC8DIR%
  set PROJ=quake3\code\win32\msvc8
)

echo Set devenv path.
call "%VCDIR%\Bin\vcvars32.bat"

echo Building Project.
devenv "%PROJ%\quake3.sln" /build debug


REM Making Bin directory

echo Making sure bin dir exists...

IF NOT EXIST bin_debug\NUL md bin_debug

echo Copying executable and dlls...
copy /Y %PROJ%\build\quake3_debug\ioquake3.exe bin_debug\ioquake3.exe
copy /Y %PROJ%\build\game_debug\qagamex86.dll bin_debug\qagamex86.dll
copy /Y %PROJ%\build\cgame_debug\cgamex86.dll bin_debug\cgamex86.dll
copy /Y %PROJ%\build\q3_ui_debug\uix86.dll bin_debug\uix86.dll

echo Making sure genetic dir exists...
IF NOT EXIST bin_debug\genetic\NUL md bin_debug\genetic

echo Making sure baseq3 dir exists...
IF NOT EXIST bin_debug\baseq3\NUL md bin_debug\baseq3

echo Copying dlls to baseq3 and genetic...
copy /Y bin_debug\*.dll bin_debug\genetic\
copy /Y bin_debug\*.dll bin_debug\baseq3\

echo Copying genetic files to genetic...
xcopy /S /E /Y resources\genetic\* bin_debug\genetic\
xcopy /S /E /Y resources\genetic\* bin_debug\baseq3\

echo Copying run script...
copy /Y resources\run.bat bin_debug\run.bat

IF NOT EXIST bin_debug\baseq3\pak0.pk3 echo Don't forget to copy the pak files to bin\baseq3.

